package com.lmonkey.filter;

import com.lmonkey.dao.Basedao;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter("/admin/admin_douserupdate")
public class DoUserUpate implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest reqq = (HttpServletRequest)req;
        HttpServletResponse respp = (HttpServletResponse) resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");

        String id = reqq.getParameter("userid");
        String cp = reqq.getParameter("cpage");
        String keywords = reqq.getParameter("keywords");

        String username = reqq.getParameter("username");

        PrintWriter out = respp.getWriter();
        //验证用户名是否为空
        if (username.equals("")){
            out.println("<script>alert('用户名不能为空');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("[a-zA-Z\\u4E00-\\u9FA5]*");
            Matcher mt = pt.matcher(username);
            if (!mt.matches()){
                out.println("<script>alert('用户名格式不正确');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
                out.close();
                return;
            }else {
                String name1 = reqq.getParameter("idd");
                Connection conn = Basedao.getconn();
                PreparedStatement ps = null;
                ResultSet rs = null;
                int count = 0;
                try {
                    String sql = "select count(*) from lmonkeyshop where user_id = ? and user_id != ? ";
                    ps = conn.prepareStatement(sql);
                    ps.setObject(1,username);
                    ps.setObject(2,name1);
                    rs = ps.executeQuery();
                    while(rs.next()){
                        count = rs.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (count>0){
                    out.println("<script>alert('用户名已存在');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
                    out.close();
                    return;
                }
            }
        }
        //验证用户姓名是否为空
        String name = reqq.getParameter("name");
        if (name.equals("")){
            out.println("<script>alert('用户姓名不能为空');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile("[\\u4e00-\\u9fa5]*");
            Matcher mt = pt.matcher(name);
            if (!mt.matches()){
                out.println("<script>alert('用户姓名格式不正确');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
                out.close();
                return;
            }
        }
        //验证邮箱
        String email = reqq.getParameter("email");
        if (!email.equals("")){
            Pattern pt = Pattern.compile("^\\w+@[0-9a-z]+\\.[a-z]+$");
            Matcher mt = pt.matcher(email);
            if (!mt.matches()){
                out.println("<script>alert('邮箱格式不正确');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
                out.close();
                return;
            }
        }
        //验证手机号码
        String mobile = reqq.getParameter("mobile");
        if (mobile.equals("")){
            out.println("<script>alert('手机号码不能为空');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("^[1][3,5,7,8]\\d{9}$");
            Matcher mt = pt.matcher(mobile);
            if (!mt.matches()){
                out.println("<script>alert('手机号码格式不正确');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
                out.close();
                return;
            }
        }
        //验证收货地址
        String address = reqq.getParameter("address");
        if (address.equals("")){
            out.println("<script>alert('收货地址不能为空');location.href='/lyq/admin/admin_touserupdate?cp="+cp+"&id="+id+"&keywords="+keywords+"';</script>");
            out.close();
            return;
        }
        //不通过则直接写一个return;
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
